import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/page/analysis/widget/button_group.dart';
import 'package:boss/ui/widget/charts/pie/pie_chart.dart';
import 'package:boss/ui/widget/shadow_radius_card.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';

class RevenuePie extends StatefulWidget {
  final List<PieData> data;
  final String title;
  final List<String> action;
  final ValueChanged<int> onActionTap;
  final int defaultSelectedIndex;
  final int showLabelIndex;
  final ValueChanged<int> onLabelSelectedChanged;

  const RevenuePie({
    Key key,
    this.data,
    this.title,
    this.action,
    this.onActionTap,
    this.defaultSelectedIndex = 0,
    this.showLabelIndex,
    this.onLabelSelectedChanged,
  }) : super(key: key);

  @override
  _RevenuePieState createState() => _RevenuePieState();
}

class _RevenuePieState extends State<RevenuePie> {
  int index;

  List<bool> get selected {
    List<bool> temp = [];

    for (int i = 0; i < widget.action.length; i++) {
      temp.add(i == index);
    }

    return temp;
  }

  List<PieData> get data => widget.data.where((item) => item.value > 0).toList();

  num get total {
    num temp = 0;
    data.forEach((item) {
      temp += item.value;
    });

    return temp;
  }

  @override
  void initState() {
    super.initState();

    index = widget.defaultSelectedIndex;
  }

  @override
  Widget build(BuildContext context) {
    return ShadowRadiusCard(
      margin: EdgeInsets.only(
        bottom: 30.w,
      ),
      padding: EdgeInsets.only(
        top: 30.w,
        left: 20.w,
        right: 20.w,
      ),
      child: Column(
        children: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Text(
                widget.title,
                style: TextStyleConstant().normal_1_34,
              ),
              widget.action == null
                  ? SizedBox()
                  : ButtonGroup(
                      buttons: widget.action,
                      onChanged: widget.onActionTap,
                      selectedIndex: widget.defaultSelectedIndex,
                      height: 50.w,
                      width: 200.w,
                      normalTextStyle: TextStyleConstant().blue_24,
                      actionTextStyle: TextStyle(
                        fontSize: 24.sp,
                        color: Colors.white,
                        fontWeight: FontWeight.w500,
                      ),
                    ),
            ],
          ),
          Container(
            margin: EdgeInsets.only(
              top: 28.w,
            ),
            child: PieChart<int>(
              data: widget.data,
              labelBuilder: (item) => '${item.label}\n${widget.defaultSelectedIndex == 0 ? AmountUtil.price2String(item.value) : item.value}',
              showLabelIndex: widget.showLabelIndex,
              onLabelSelectedChanged: widget.onLabelSelectedChanged,
            ),
          ),
          for (final item in widget.data)
            DefaultTextStyle(
              style: TextStyleConstant().normal_1_28,
              child: Container(
                height: 80.w,
                padding: EdgeInsets.symmetric(
                  horizontal: 10.w,
                ),
                child: Row(
                  children: <Widget>[
                    Expanded(child: Text(item.label)),
                    Expanded(child: Center(child: Text('${mathValue(item.value)}'))),
                    Expanded(
                        child: Text(
                      '${mathAccounted(item.value)}',
                      textAlign: TextAlign.end,
                    )),
                  ],
                ),
              ),
            ),
        ],
      ),
    );
  }

  String mathAccounted(num value) {
    if (total == 0) {
      return '0.00%';
    }
    return (value / total * 100).toStringAsFixed(2) + '%';
  }

  String mathValue(num value) {
    if (widget.defaultSelectedIndex == 0) {
      return AmountUtil.price2String(value);
    } else {
      return '$value';
    }
  }
}
